//
//  ProductContentView.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/5/27.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import RxSwift

class ProductContentView: UIView {
    
//    @IBOutlet weak var lblName: UILabel!
//
//    @IBOutlet weak var lblNo: UILabel!
//
//    @IBOutlet weak var lblPrice: UILabel!
//
//    @IBOutlet weak var lblBuyCount: UILabel!
//
//    @IBOutlet weak var lblLeftCount: UILabel!
//
//    @IBOutlet weak var lblDZCount: UILabel!
//
//    @IBOutlet weak var vwDz: UIView!
    
    var lblName: UILabel!
    var lblNo: UILabel!
    var lblPrice: UILabel!
    var lblBuyCount: UILabel!
    var lblDZCount: UILabel!
    var vwDz: UIView!
    var lblDZ:UILabel!
    let disposeBag = DisposeBag()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        lblName = self.viewWithTag(1) as! UILabel
        lblNo = self.viewWithTag(2) as! UILabel
        lblPrice = self.viewWithTag(3) as! UILabel
        lblBuyCount = self.viewWithTag(4) as! UILabel
        lblDZCount = self.viewWithTag(6) as! UILabel
        vwDz = self.viewWithTag(7)!
        lblDZ = self.viewWithTag(8) as! UILabel
        
        let tap = UITapGestureRecognizer()
        tap.rx.event.subscribe(onNext:{ [unowned self] _ in
            if self.isCanDZ == false {
                return
            }
            
            guard let _ = MainTools.shared.userModel else {
                self.parentViewController?.navigationController?.tabBarController?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
                self.parentViewController?.navigationController?.tabBarController?.present((UIStoryboard(name: "LoginAndRegister", bundle: nil).instantiateInitialViewController() as? UINavigationController) ?? UINavigationController(), animated: true, completion:nil)
                return
            }
            
            SVProgressHUD.show()
            //进行点赞操作
            NetworkTool.requestData(url: ActionURL.DZ_COMMODITY, param: ["goods_id":self.model.id])
                .map({ (json) -> Bool in
                    return true
                }).catchErrorJustReturn(false)
                .observeOn(MainScheduler.instance)
                .subscribe(onNext:{ [unowned self] isSuccess in
                    SVProgressHUD.dismiss()
                    if isSuccess {
                        self.isCanDZ = false
                        self.lblDZ.text = "已赞"
                        self.lblDZCount.text = self.makeDzIncrease()
                        self.vwDz.backgroundColor = .lightGray
                        MainTools.shared.saveDzDataByUser(uid: MainTools.shared.userModel?.uid ?? "", cid: self.model.id)
                    }
                }).disposed(by: self.disposeBag)
        }).disposed(by: disposeBag)
        vwDz.addGestureRecognizer(tap)
    }
    
    var isCanDZ:Bool = true
    
    var model:CommodityDetailModel = CommodityDetailModel() {
        didSet {
            lblName.text = model.name
            lblNo.text = "商品编号：\(model.id)"
            lblPrice.text = "¥ \(model.price)"
            lblBuyCount.text = "库存还剩：\(model.num)件"
            lblDZCount.text = "(\(model.fabulous_num))"
            
            self.isCanDZ = !MainTools.shared.isDzByUser(uid: MainTools.shared.userModel?.uid ?? "", cid: model.id)
            vwDz.backgroundColor = self.isCanDZ ? UIColor(hex: 0xFFAB18) : .lightGray
            lblDZ.text = self.isCanDZ ? "点赞" : "已赞"
        }
    }
    
    func makeDzIncrease() -> String {
        var dzCount = Int(model.fabulous_num) ?? 0
        dzCount += 1
        return "(\(dzCount))"
    }
    
}
